package com.ruoyi.sqlbuilder.execute.factory;

import com.ruoyi.sqlbuilder.execute.constants.ConvertConstants;
import com.ruoyi.sqlbuilder.execute.convert.ISqlHandler;
import com.ruoyi.sqlbuilder.execute.handler.DmSqlHandler;
import com.ruoyi.sqlbuilder.execute.handler.HigHgoSqlHandler;
import com.ruoyi.sqlbuilder.execute.handler.KingBaseSqlHandler;
import com.ruoyi.sqlbuilder.execute.handler.OscarSqlHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * sql转换执行器工厂
 *
 * @author Yanzy
 */
public class SqlExecuteFactory {

    private static Logger log = LoggerFactory.getLogger(SqlExecuteFactory.class);

    private static Map<String, Class> factory;

    static {
        factory = new ConcurrentHashMap<>();
        // dm数据库转换器
        factory.put(ConvertConstants.DB_DM, DmSqlHandler.class);
        // kingBase
        factory.put(ConvertConstants.DB_KING_BASE, KingBaseSqlHandler.class);
        // highgo
        factory.put(ConvertConstants.DB_HIGHGO, HigHgoSqlHandler.class);
        // oscar
        factory.put(ConvertConstants.DB_OSCAR, OscarSqlHandler.class);
    }

    public static ISqlHandler invoke(String type) {

        ISqlHandler handler = null;
        try {
            if (factory.containsKey(type)) {
                handler = (ISqlHandler) factory.get(type).newInstance();
            }
        } catch (Exception e) {
            log.error("获取 {} 转换器实例错误: {}", type, e.getMessage());
        }
        return handler;
    }
}
